package com.ldwx.center.controller;

import com.ldwx.center.common.entity.PageResult;
import com.ldwx.center.common.entity.Result;
import com.ldwx.center.common.entity.StatusCode;
import com.ldwx.center.model.Company;
import com.ldwx.center.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/company")

public class CompanyController {

    @Autowired
    private CompanyService service;

    /**
     * 添加公司
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result addCompany(@RequestBody Company company) {
        service.add(company);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 删除公司
     * @param companyId
     * @return
     */
    @RequestMapping(value = "/{companyId}", method = RequestMethod.DELETE)
    public Result deleteCompany(@PathVariable String companyId) {
        service.delete(companyId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改公司
     * @param companyId
     * @param company
     * @return
     */
    @RequestMapping(value = "/{companyId}", method = RequestMethod.PUT)
    public Result updateCompany(@PathVariable String companyId, @RequestBody Company company) {
        service.update(company);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 查询用户所有公司
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAllCompany() {
        List<Company> list = service.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据id查询公司信息
     * @param companyId
     * @return
     */
    @RequestMapping(value = "/{companyId}", method = RequestMethod.GET)
    public Result findByIdCompany(@PathVariable String companyId) {
        Company company = service.findById(companyId);
        return new Result(true, StatusCode.OK, "查询成功", company);
    }
}
